<?php
class commentService extends commonService{
	private $comment_limit=30;
	private $comment_time=30;
	
	private $comment_min=5;
	private $comment_max=300;
	
	private $comment_status=0;//1显示 0隐藏
	
	public function __construct(){
		parent::__construct();
		
		$this->comment_limit=$this->conf->arc_comment['comment_limit'];
		$this->comment_time=$this->conf->arc_comment['comment_time'];
		$this->comment_min=$this->conf->arc_comment['comment_min'];
		$this->comment_max=$this->conf->arc_comment['comment_max'];
		$this->comment_status=$this->conf->arc_comment['comment_status'];
		
		$this->table=table('comment');
		$this->table2=table('comment_stat');
		
		$this->table_arc=table('arc');
	}
	public function hide_ip($ip, $sep = '*') {
		if(!preg_match("/[\d\.]{7,15}/", $ip)) return $ip;
		$tmp = explode('.', $ip);
		return $tmp[0].'.'.$tmp[1].'.'.$sep.'.'.$sep;
	}
	//检查是否允许评论
	public function is_allow_comment($content){
		$code=1;
		$ip=ip();//获取ip
		$today=strtotime(date('Y-m-d', time()).' 23:59:59')-86400;
		$extra=" and `ip`='{$ip}'";
		$extra.=" and `timestring`>'{$today}'";
		if($this->comment_limit>0){
			$query="select count(*) from `{$this->table}` where 1 {$extra}";
			$r=$this->m->get_field($query);
			($r>$this->comment_limit)?$code=-1:'';
			if($code<0) return $code;
		}
		if($this->comment_time>0){
			$query="select `timestring` from `{$this->table}` where 1 {$extra} order by `timestring` desc limit 1";
			$r=$this->m->get_field($query);
			($r && time()-$r<$this->comment_time)?$code=-2:'';
			if($code<0) return $code;
		}
		$length=utf8_strlen($content);
		if($length<$this->comment_min){
			$code=-3;
			if($code<0) return $code;
		}
		if($length>$this->comment_max){
			$code=-4;
			if($code<0) return $code;
		}
		return $code;
	}
	//处理正文
	public function treat_content($content){
		$content=htmlspecialchars(trim($content),ENT_QUOTES);
		$content=preg_replace("/&([a-z]{1,});/", '', $content);
		$content=nl2br($content);
		return $content;
	}
	//取得引用数据
	public function get_q($qid,$content){
		$query="select `ip`,`hidden`,`username`,`content`,`quotation`,`timestring` from `{$this->table}` where `id`='{$qid}' limit 1";
		$r=$this->m->select($query);
		$quotation = '';
		if($r) {
			$r['name'] = 'IP:'.$this->hide_ip($r['ip']);
			$r['timestring']=date($r['timestring'],'Y-m-d H:i:s');
			if($r['quotation']) $r['content']=$r['quotation'];
			$floor=substr_count($r['content'],'quote_content')+1;
			if($floor==1){
				$quotation=addslashes('<div class="quote"><div class="quote_title"><span class="quote_floor">'.$floor.'</span>'.$r['name'].' 于 <span class="quote_time">'.$r['timestring'].'</span> 评论道：</div><div class="quote_content">'.$r['content'].'</div><!----></div>').$content;
			}else{
				$quotation = str_replace('<!----></div>', '</div><div class="quote_title"><span class="quote_floor">'.$floor.'</span>'.$r['name'].' 于 <span class="quote_time">'.$r['timestring'].'</span> 评论道：</div><div class="quote_content">', $r['content']);
				$quotation = '<div class="quote">'.$quotation.'</div><!----></div>';
				$quotation = addslashes($quotation).$content;
			}
		}
		$query="update `{$this->table}` set `quote`=`quote`+1 where `id`='{$qid}' limit 1";
		$this->m->query($query);
		return $quotation;
	}
	//插入评论
	public function add_($post){
		$post['mid']=intval($post['mid']);
		
		$post['arc_id']=intval($post['arc_id']);
		//$post['arc_title']='';
		//$post['arc_username']='';
		
		$post['content']=$this->treat_content($post['content']);

		$post['qid']=intval($post['qid']);
		if($post['qid']>0){
			$post['quotation']=$this->get_q($post['qid'],$post['content']);
		}else{
			$post['quotation']='';
		}
		
		$post['timestring']=time();
		$post['username']='';
		$post['hidden']='1';
		
		$post['ip']=ip();
		$post['status']=$this->comment_status;
		
		$post['star']=intval($post['star']);
		$post['star']=in_array($post['star'],array(1,2,3))?$post['star']:3;
		
		$vali=validate::check('comment',$post,'add');
		if($vali){
            $result[0]=$this->model->_i($this->table,$post);
			if($result[0]){
				$this->update_stat($post['arc_id'],$post['star']);
			}
        }else{
            $result[0]=false;
            $result[1]="\\n".implode("\\n",validate::getMsg());
        }
        return $result;
	}
	//更新stat
	private function update_stat($arc_id,$star){
		$query="select `id` from `{$this->table2}` where `arc_id`='{$arc_id}' limit 1";
		$r=$this->m->get_field($query);
		$star='star'.$star;
		if($r){
			$query="update `{$this->table2}` set `comment`=`comment`+1,`{$star}`=`{$star}`+1 where `id`='{$r['id']}' limit 1";
		}else{
			$query="insert into `{$this->table2}` (`arc_id`,`{$star}`,`comment`) VALUES ('{$arc_id}','1','1')";
		}
		return $this->m->query($query);
	}
	//取得一篇文章的详细信息
	public function get_arc($arc_id){
		$query="select `id`,`title`,`add_man`,`status`,`htmlurl`,`allowcomment` from `{$this->table_arc}` where `id`='{$arc_id}' limit 1";
		return $this->m->select($query);
	}
	//取得该文章的评论统计
	public function mystat($arc_id){
		$query="select * from `{$this->table2}` where `arc_id`='{$arc_id}' limit 1";
		$stat=$this->m->select($query);
		if($stat && $stat['comment']) {
			$stat['pc1'] = round($stat['star1']*100/$stat['comment'],2).'%';
			$stat['pc2'] = round($stat['star2']*100/$stat['comment'],2).'%';
			$stat['pc3'] = round($stat['star3']*100/$stat['comment'],2).'%';
		}else{
			$stat['star1'] = $stat['star2'] = $stat['star3'] = 0;
			$stat['pc1'] = $stat['pc2'] = $stat['pc3'] = '0%';
		}
		return $stat;
	}
	//取得该文章的评论
	public function mylist($condition=array(),$extra=''){
		$pagesize=10;
        $return=array();
		//字段列表需优化
        $query="select * from `{$this->table}` where 1 {$extra} order by `id` asc";
        $p=new smart_pager($query,$this->m,$pagesize);
        //$p->address('&block_id=%block_id%','block_id',$condition['block_id']);
		foreach($condition as $k=>$v){
			$p->address("&{$k}=%{$k}%",$k,$v);
		}
        if($p->total_num>0){
            $return[]=$this->m->mselect($p->query);
            $return[]=$p;
			
			isset($_GET['page_id'])?'':$_GET['page_id']=1;
			$page_id=intval($_GET['page_id']);
			$page_id=max($page_id,1);
			$floor = $page_id == 1 ? 0 : ($page_id-1)*$pagesize;
			foreach($return[0] as $k=>$v){
				$return[0][$k]['floor']=++$floor;
				$return[0][$k]['name']='IP:'.$this->hide_ip($v['ip']);
			}
        }
		
        return $return;
    }
	//更新vote数据
	public function up_vote($id,$op){
		$f = $op ? 'agree' : 'against';
		$query="update `{$this->table}` set `{$f}`=`{$f}`+1 where `id`='{$id}' limit 1";
		return $this->m->query($query);
	}
	//跳转到最后一页
	public function comment_jump(){
		//$forward = 'comment.php?mid='.$mid.'&arc_id='.$arc_id.'&page_id='.$page_id.'&rand='.mt_rand(10, 99).'#last';
		//dalert('', '', 'parent.window.location="'.$forward.'";');
	}
//
}